import { ChooseScanType } from '@renderer/views/multiScanSeriesPage/helper/types'
import { initBallsPosition } from '@renderer/views/multiScanSeriesPage/helper/helper'
import { useScanStore } from '@renderer/store/pinia/scan'

type scanSeriesParamsType = {
  [key: string]: {
    value: any,
    units: string
  }
}

export enum EnumScanStatus {
  c_not_fit = 'condition_not_fit', // 前置条件不满足，不可以执行扫描
  c_fit = 'condition_fit',  // 前置条件满足，可以执行扫描
  scanning = 'scanning', //  扫描指令刚下发，
  scan_img_folder_created = 'scan_img_folder_created', //影像的文件夹已经创建，此时
  wait_img = 'wat_img',  // 等待图像中
  getting_img = 'getting_img', // 获取图像中。
  finish = 'finish'  //完成
}

type typeImgProgress = {
  currentCount: number,
  totalImgCount: number
}

export type scanSeriesType = {
  id?: string,
  scanSeriesName: string,
  keywords: any,
  scanType: ChooseScanType,
  params: {
    [key: string]: scanSeriesParamsType
  },
  status?: EnumScanStatus, // finished、scanning、 wait2Scan_conditionFit、wait2Scan_conditionNotFit
  hasScoutImage?: boolean, // 是否平片出图
  scoutCount?: number,
  eachScoutMap?: Map<string, typeImgProgress>, // 如果是单平片就一个id。 双平片就两个。
  hasImage?: boolean, //是否出图
  ballsInfo?: any, // 扫描位置信息
  normalImage?: any,
  prevRelatedSeries?: number[], //当前扫描执行所前置需要的扫描。
  nextRelatedSeries?: number[], //当前扫描执行后，能影响到的扫描。
  imageIds: []
}

export class BaseScanSeries {
  id: string
  scanSeriesName: string
  keywords: any
  scanType: ChooseScanType
  params: {
    [key: string]: scanSeriesParamsType
  }
  status: any // finished、scanning、 wait2Scan_conditionFit、wait2Scan_conditionNotFit
  hasScoutImage: boolean // 是否平片出图
  scoutCount: number  //平片的数量: 1 / 2
  prevRelatedSeries: (number | string)[]
  nextRelatedSeries: (number | string)[]
  eachScoutMap: Map<string, typeImgProgress>
  imageIds: (string | number)[]

  constructor(scanSeries: scanSeriesType) {
    this.id = scanSeries.id || this.getId(scanSeries.scanType)
    this.scanSeriesName = scanSeries.scanSeriesName
    this.keywords = scanSeries.keywords
    this.scanType = scanSeries.scanType
    this.params = scanSeries.params || {}
    this.status = scanSeries.status
    this.hasScoutImage = scanSeries.hasScoutImage || false
    this.scoutCount = scanSeries.scoutCount || 0
    this.prevRelatedSeries = scanSeries.prevRelatedSeries || []
    this.nextRelatedSeries = scanSeries.nextRelatedSeries || []
    this.eachScoutMap = new Map()
    this.imageIds = []
  }


  getId(scanType: ChooseScanType): string {
    const now = new Date()

// 获取年、月、日
    const year = now.getFullYear()
    const month = now.getMonth() + 1 // 注意：月份从 0 开始，需要加 1
    const day = now.getDate()

// 获取时、分、秒
    const hours = now.getHours()
    const minutes = now.getMinutes()
    const seconds = now.getSeconds()

    const formattedDate = `${year}${month < 10 ? '0' + month : month}${day < 10 ? '0' + day : day}-${hours < 10 ? '0' + hours : hours}${minutes < 10 ? '0' + minutes : minutes}${seconds < 10 ? '0' + seconds : seconds}`

    return `${scanType}_${formattedDate}`
  }

  receiveScoutImage() {
    if (!this.hasScoutImage) {
      this.hasScoutImage = true
      this.status = EnumScanStatus.getting_img
      this.updateRelateSeriesAfterScout()
    }
  }

  // 如果同时添加了一个平片、一个螺旋/断层。当平片扫描完成，那么螺旋/断层的状态也应该变为平片出图后。
  updateRelateSeriesAfterScout() {
    if (this.nextRelatedSeries.length === 0) {
      return
    }
    const { points, centerOp } = initBallsPosition()
    const scanStore = useScanStore()
    this.nextRelatedSeries.map((seriesId: number | string) => {
      const relatedSeries = scanStore.scanSeriesInfo.getSeriesById(seriesId)
      if (relatedSeries instanceof NormalScanSeries) {
        let ballsInfo = {}
        relatedSeries.scoutImageIds = this.imageIds.slice(0);
        this.imageIds.forEach((imageId, _) => {
          ballsInfo[imageId] = {
            points: structuredClone(points),
            centerOp: structuredClone(centerOp)
          }
        })
        relatedSeries!.hasScoutImage = true
        relatedSeries.status = EnumScanStatus.c_fit
        relatedSeries.ballsInfo = structuredClone(ballsInfo)
      }
    })
  }

}

export class NormalScanSeries extends BaseScanSeries {
  hasImage: boolean //是否出图
  ballsInfo: any // 扫描位置信息
  normalImage: any
  scoutImageIds: any[]

  constructor(scanSeries: scanSeriesType) {
    super(scanSeries)
    this.hasImage = scanSeries.hasImage || false
    this.ballsInfo = scanSeries.ballsInfo
    this.normalImage = scanSeries.normalImage || []
    this.scoutImageIds = scanSeries.imageIds
  }
}


export const paramsType = {
  kV: {
    showType: 'dropdown',
    values: [10, 20, 30, 40],
    order: 1
  },
  mA: {
    showType: 'dropdown',
    values: [10, 20, 30, 40],
    order: 2
  },
  start: {
    showType: 'inputNumber',
    order: 3,
    limit: {
      max: 120,
      min: 90,
      precision: 1
    }
  },
  end: {
    showType: 'input',
    order: 4,
    limit: {
      max: 120,
      min: 90,
      precision: 1
    }
  },
  fov: {
    showType: 'input',
    order: 5,
    limit: {
      max: 120,
      min: 90,
      precision: 1
    }
  },
  windowWidth: {
    showType: 'input',
    order: 6,
    limit: {
      max: 120,
      min: 90,
      precision: 1
    }
  },
  windowCenter: {
    showType: 'input',
    order: 7,
    limit: {
      max: 120,
      min: 90,
      precision: 1
    }
  },
  angle: {
    showType: 'disabled',
    order: 8

  },
  rotateTime: {
    showType: 'dropdown',
    values: [1, 2, 3, 4],
    order: 9
  },
  focus: {
    showType: 'dropdown',
    values: ['big', 'small'],
    order: 10
  },
  reconstructMatrix: {
    showType: 'dropdown',
    values: [512, 1024],
    order: 11
  },
  sliceThickness: {
    showType: 'dropdown',
    values: [0.6, 1.2, 2],
    order: 12
  },
  pitch: {
    showType: 'disabled',
    order: 13
  },
  collimator: {
    showType: 'disabled',
    order: 14
  },
  part: {
    showType: 'disabled',
    order: 15
  },
  monitorDelay: {
    showType: 'dropdown',
    values: [1, 2, 3, 4],
    order: 16
  },
  monitorInterval: {
    showType: 'dropdown',
    values: [1, 2, 3, 4],
    order: 17
  },
  enhanceThreshold: {
    showType: 'dropdown',
    values: [1, 2, 3, 4],
    order: 18
  },
  exposureTimes: {
    showType: 'dropdown',
    values: [1, 2, 3, 4],
    order: 19
  }
}
